<?php
/////////////////////////////////////////////////////////////////////////////
// 
//
// workflow_system
//
// workflow_copyright
// 
/////////////////////////////////////////////////////////////////////////////

/**
  * FILE_NAME : attribute.class.php   FILE_PATH : E:\www\\trunk\classes\application\attribute.class.php
  * ....商品属性操作类
  *
  * @copyright workflow_system 
  * @author workflow Develop Team 
  * @package 
  * @subpackage 
  * @version Mon Jun 23 21:36:34 CST 2008
  */ 
defined('Base_Use_Sign') or die("this is invalid system!please contact <a href='http://www.shopnc.net'>www.shopnc.net</>.");

class AttributeClass extends FrameWork {
	var $obj_attribute_content;

	/**
	 * 得到属性列表
	 *
	 * @param int $pc_id 商品分类ID
	 * @return array $attribute_array 返回属性列表数组
	 */
	function getAttributeList($obj_condition,&$obj_page){
		$condition_str = $this->_conditionSearch($obj_condition);
		$attribute_array = $GLOBALS['db']->GetList($condition_str, $obj_page, "attribute");
		return $attribute_array;
	}
	/**
	 * 得到带属性内容的属性列表
	 *
	 * @param int $pc_id 商品分类ID
	 * @return $attribute_with_content_array 返回带属性内容的属性列表
	 */
	function getAttributeWithContentList($condition,&$obj_page){
		/**
		 * 创建商品属性对象
		 */
		if(!is_object($this->obj_attribute_content)){
			require_once("attribute_content.class.php");
			$this->obj_attribute_content = new AttributeContentClass();
		}
		$condition_str = $this->_conditionSearch($condition);
		$attribute_array = $GLOBALS['db']->GetList($condition_str, $obj_page, "attribute");
		for($i=0;$i<count($attribute_array);$i++){
			$condition_attribute_content['a_id'] = $attribute_array[$i]['a_id'];
			$attribute_with_content_array[$i] = $this->obj_attribute_content->getAttributeContentList($condition_attribute_content,$page);
			unset($condition_attribute_content);
		}
		return $attribute_with_content_array;
	}
	/**
	 * 得到单一属性
	 *
	 * @param unknown_type $a_id
	 * @return unknown
	 */
	function getAttributeRow($a_id){
		if (intval($a_id) > 0){
			$product_row = $GLOBALS['db']->GetOneRow($a_id,'attribute','a_id');
			return $product_row;
		}else {
			return false;
		}
	}
	/**
	 * 添加商品分类的属性
	 *
	 * @param array $input
	 * @return boolen
	 */
	function addAttribute($input){
		$valueArray = array();
		$valueArray["pc_id"]	= $input["txtApcid"];
		$valueArray["a_name"]	= $input["txtAname"];
		$valueArray["a_type"]	= $input["txtAtype"];
		$valueArray["a_allow_inherit"]	= $input["txtAinherit"];
		$valueArray["a_info"]	= $input["txtAinfo"];
		$valueArray["a_order"]	= $input["txtAorder"];
		$valueArray["a_time"]	= $input["a_time"];
		$insert_rs = $GLOBALS['db']->InsertRow($valueArray, 'attribute', 'a_id');
		return $insert_rs;
	}
	/**
	 * 更新商品分类的属性
	 *
	 * @param array $input
	 * @return boolen
	 */
	function updateAttribute($input){
		if (intval($input["a_id"]) > 0){
			$update_rs = $GLOBALS['db']->UpdateRow(intval($input["a_id"]), $input, 'attribute', 'a_id');
			return $update_rs;
		}else {
			return false;
		}
	}
	/**
	 * 删除商品分类的属性
	 *
	 * @param array $input
	 * @return boolen
	 */
	function delAttribute($input){
		if ($input){
			$del_rs = $GLOBALS['db']->DelRow($input, 'attribute', 'a_id');
			return $del_rs;
		}else {
			return false;
		}
	}
	/**
	 * 构造属性检索条件
	 *
	 * @param object $obj_condition 查询条件对象
	 * @return string $conditionStr 返回查询条件SQL字符串
	 */
	function _conditionSearch($condition){
		$conditionStr = "";
		//商品类别
		if ($condition['pc_id'] != ''){
			$conditionStr .= " and pc_id = '" . $condition['pc_id'] . "'";
		}
		//属性ID范围内
		if ($condition['in_a_id'] != ''){
			$conditionStr .= " and a_id in (" . $condition['in_a_id'] . ")";
		}
		//属性ID
		if ($condition['a_id'] != ''){
			$conditionStr .= " and a_id = '" . $condition['a_id'] . "'";
		}
		//时间
		if ($condition['a_time'] != ''){
			$conditionStr .= " and a_time = '" . $condition['a_time'] . "'";
		}
		if($condition['order'] == 1){
			$conditionStr .= " order by a_order desc";
		}else{
			if ($condition['order'] == 'a_id_desc'){
				$conditionStr .= " order by a_id desc";
			}else {
				$conditionStr .= " order by a_order";
			}
		}
		return $conditionStr;
	}

	/**
	 * 取最大ID
	 * @author workflow Develop Team     
	 * @param  
	 * @return int
	 */
	function getLastId(){
		$attribute_array = $GLOBALS['db']->GetLastRow('attribute','a_id','');
		$last_id = $attribute_array['a_id'];
		unset($attribute_array);
		return $last_id;
	}

	/**
	 * 取父类属性及内容
	 * @author workflow Develop Team     
	 * @param  intval $pc_id
	 * @return array
	 */
	function getParentAttributeContent($pc_id){
		/**
		 * 创建商品分类对象
		 */
		if (!is_object($obj_pclass)) {
			require_once('productclass.class.php');
			$obj_pclass = new ProductCategoryClass();
		}
		/**
		 * 创建属性内容对象
		 */
		if (!is_object($obj_attribute_content)) {
			require_once('attribute_content.class.php');
			$obj_attribute_content = new AttributeContentClass();
		}
		$pc_id = intval($pc_id);
		//取当前商品分类信息
		$pclass_array = $obj_pclass->getPClassRow($pc_id);
		//取父类属性，判断是否允许继承，允许继承则该子类继承父类属性
		if ($pclass_array['pc_u_id'] != 0) {
			//取分类列表
			$pclass_list = $this->getParent($pclass_array['pc_u_id'],$pclass_list);
			//取父类属性
			if (is_array($pclass_list)) {
				foreach ($pclass_list as $k1 => $v1){
					$parent_arr_list = $this->getAttributeList(array('pc_id'=>$v1['pc_id']),$page);
					if (!empty($parent_arr_list)) {
						foreach ($parent_arr_list as $k2 => $v2){
							if ($v2['a_allow_inherit'] != 0) {  //允许继承
								$parent_att_array[] = $v2;
								$parent_att_array[count($parent_att_array)-1]['content'] = $obj_attribute_content->getAttributeContentList(array('a_id'=>$v2['a_id']),$page);
							}
						}
					}
				}
			}
			//取当前分类的属性及属性内容
			$now_att_list = $this->getAttributeList(array('pc_id'=>$pc_id),$page);
			if (!empty($now_att_list)) {
				foreach ($now_att_list as $k3 => $v3){
					$now_att_list[$k3]['content'] = $obj_attribute_content->getAttributeContentList(array('a_id'=>$v3['a_id']),$page);
				}
			}
			//判断父类属性及当前分类属性是否为空
			if (empty($now_att_list) && empty($parent_att_array)) {
				$att_array = array();
			}elseif (!empty($now_att_list) && !empty($parent_att_array)) {
				//合并父类属性和当前分类属性
				$att_array = array_merge($now_att_list,$parent_att_array);
				unset($pclass_list,$parent_arr_list);
			}elseif (empty($now_att_list)){
				return $parent_att_array;
			}elseif (empty($parent_att_array)){
				return $now_att_list;
			}
		}else {
			$att_array = $this->getAttributeList(array('pc_id'=>$pclass_array['pc_id']),$page);
			if (!empty($att_array)) {
				foreach ($att_array as $k => $v){
					$att_array[$k]['content'] = $obj_attribute_content->getAttributeContentList(array('a_id'=>$v['a_id']),$page);
				}
			}
		}
		unset($pclass_array);
		return $att_array;
	}
	/**
	 * 递归取父类
	 * @author workflow Develop Team     
	 * @param  intval $parent_id 父类ID array $class_list
	 * @return array
	 */
	function getParent($parent_id,$class_list=array()){
		/**
		 * 创建商品分类对象
		 */
		if (!is_object($obj_pclass)) {
			require_once('productclass.class.php');
			$obj_pclass = new ProductCategoryClass();
		}
		//取父类信息
		$class = $obj_pclass->getPClassRow($parent_id);
		$class_list[] = $class;
		if ($class['pc_u_id'] != 0) {
			$class_list = $this->getParent($class['pc_u_id'],$class_list);
		}
		unset($class);
		return $class_list;
	}
}
?>